package ex55.mm.flow.impound.quote.common.kit;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * @author zhouzhongyuan
 * @since 2017/4/19
 */
public class FriendlyException extends RuntimeException {
    private final int code;
    private final String message;
    private final Object data;
    private Throwable causeException;


    public FriendlyException(int code, String message) {
        this(code, message, null);
    }


    public FriendlyException(Throwable causeException) {
        this(Warning.Server_Error.getCode(), causeException.getMessage());
        this.causeException = causeException;
    }

    private FriendlyException(int code, String message, Object data) {
        super(message, null, false, false);

        this.code = code;
        this.message = message;
        this.data = data;
    }


    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    public Throwable getCauseException() {
        return causeException;
    }

    public FriendlyException(int code, Throwable causeException) {
        this(code, causeException.getMessage());
        this.causeException = causeException;
    }

    public FriendlyException duplicate(String message) {
        return new FriendlyException(this.code, message);
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        FriendlyException that = (FriendlyException) o;

        return new EqualsBuilder()
                .append(code, that.code)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(code)
                .toHashCode();
    }
}
